<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Banner extends Model
{
    protected $table = 'banners';
    public $timestamps = false;
    protected $guarded = [
        'title', 'img', 'redirect_url', 'sort', 'on_line_time', 'off_line_time', 'created_at', 'updated_at'
    ];


    public function getData($search = [], $page = 1, $length = 20){
        $bannerList = array();
        $bannerList['pageData'] = array();
        $date = date('Y-m-d H:i:s', time());

        $sql = \DB::table($this->table)
            ->whereNull('deleted_at')
            ->where('on_line_time', '<=', $date)
            ->where('off_line_time', '>', $date);

        if(isset($search['title'])){
            $sql->where('title', 'like', "%".$search['title']."%");
        }

        $bannerList['pageData']['count']     = $sql->count();
        $bannerList['pageData']['lastPage']  = ceil($bannerList['pageData']['count'] / $length);

        $bannerList['list'] = $sql->orderBy('sort', 'desc')
            ->skip(($page-1) * $length)->take($length)
            ->get();

        return $bannerList;
    }

    public function saveData($data){
        return \DB::table($this->table)->insert($data);
    }

    public function saveEdit($data,$id){
        return \DB::table($this->table)->where('id',$id)->update($data);
    }
    public function Del($data,$id){
        return \DB::table($this->table)->where('id',$id)->update($data);
    }
}